package com.kang.controller;

import com.kang.pojo.Items;
import com.kang.pojo.OrderSub;
import com.kang.service.ItemsService;
import com.kang.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author 吃三明治派对中的康康
 * @Date 2022/5/1 11:20
 * @Version 1.0
 */
@Controller
public class ShoppingController {
    @Autowired
    ShoppingService shoppingService;
    @Autowired
    ItemsService itemsService;

    @RequestMapping("/shopping")
    public String shopping(Model model){
        double pay = 0;
        List<OrderSub> orderSubs = shoppingService.searchAllOrder();
        ArrayList<Items> items = new ArrayList<>();
        for (OrderSub orderSub : orderSubs) {
            pay += orderSub.getSum();
            items.add(itemsService.selectGoodsById(orderSub.getId()));
        }
        model.addAttribute("shoppingModel",items);
        model.addAttribute("totalPayModel",pay);
        return "shopping";
    }

    @RequestMapping("/shopping/delete/{id}")
    public String shoppingDelete(@PathVariable long id){
        shoppingService.delShoppingById(id);
        return "redirect:/shopping";
    }


}
